#include "FormMenu.h"
#include "BasicFileReader.h"
#include "InputElement.h"
#include "include.h"

FormMenu::FormMenu(TextureManager * textureManager, char * menuDescriptor, Vector2D position, int index)
:Menu(NULL, position, index)
{
	CreateMenu(textureManager, menuDescriptor);	
}
FormMenu::~FormMenu()
{
}
void FormMenu::CreateMenu(TextureManager * textureManager, char * menuDescriptor)
{
	//menuDescriptor is a string in the form n,propname1,propname2,...,propnamen
	printf("Creating menu\n");
	Vector2D position = Vector2D(0,0);
	float textSize = 1.5;
	
	int pos = 0;
	int numFields = BasicFileReader::ReadNextInt(menuDescriptor,&pos);
	menuElements = new MenuElementList();
	for(int i = 0; i < numFields; i++)
	{
		printf("string = %s\n", menuDescriptor);
		printf("pos = %d\n", pos);
		char * name = BasicFileReader::ReadNextString(menuDescriptor, &pos);
		int numLines = BasicFileReader::ReadNextInt(menuDescriptor, &pos);
		printf("name = %s\n", name);
		Vector2D * textureVertices = (Vector2D*)malloc(sizeof(Vector2D)*4);
		textureVertices[0] = Vector2D(0,1);
		textureVertices[1] = Vector2D(1,1);
		textureVertices[2] = Vector2D(1,0);
		textureVertices[3] = Vector2D(0,0);
		Vector2D size = Vector2D(10, textSize * 1.5 * numLines);
		MenuElement * label = new MenuElement(false, name, textureManager, LABEL_TEXTURE, position,size,textureVertices);
		printf("making input\n");
		InputElement * input = new InputElement(textureManager, Vector2D(position.x + size.x, position.y), textSize, 50, numLines);
		printf("Adding label\n");
		menuElements->Add(label);
		printf("Adding input\n");
		menuElements->Add(input); 
		position.y += size.y * 1.2;
	}
	printf("done creating menu\n");
}
char * FormMenu::GetFormContent(int index)
{
	InputElement * selectedElement = (InputElement*)menuElements->GetMenuElement(index*2);
	if(selectedElement)
	{
		return selectedElement->GetContent();
	}
	return NULL;
}
void FormMenu::SetFormContent(int index, char * contents)
{
	InputElement * selectedElement = (InputElement*)menuElements->GetMenuElement(index*2);
	if(selectedElement)
	{
		printf("in here\n");
		return selectedElement->SetContent(contents);
	}	
}